package com.newvpn.fastbest.zenith.dialog;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.fragment.app.FragmentManager;

import com.newvpn.fastbest.zenith.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


public class TestNetDialog extends BaseDialog {

    private Button ok;

    public TestNetDialog() {

    }

    public static TestNetDialog newInstance() {
        return new TestNetDialog();
    }

    private ProgressBar progressBar;
    private Disposable disposable;

    @Override
    protected int layout() {
        return R.layout.v_dialog_test_net;
    }

    @Override
    protected void init(View view) {
//        view.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().finish();
//            }
//        });
        progressBar  = view.findViewById(R.id.progressBar);

//        countDownDisposable = Observable.interval(0, 100, TimeUnit.MILLISECONDS) // 每0.01秒发射一次
//                .map(t -> (int) (t * 100 / 30)) //
//                .take(1000)
//                .subscribeOn(Schedulers.computation()) // 在计算线程执行
//                .observeOn(AndroidSchedulers.mainThread()) // 在同一个线程上观察
//                .subscribe(
//                        progress -> {
//                            if (progress < 97) {
//
//                            }else {
//
//                            }
//                        },
//                        Throwable::printStackTrace,
//                        () -> {
//                        }
//                );
        disposable = Observable.interval(0, 100, TimeUnit.MILLISECONDS) // 每秒发射一次
                .subscribeOn(Schedulers.io()) // 在非UI线程上执行
                .observeOn(AndroidSchedulers.mainThread()) // 在UI线程上观察
                .doOnNext(aLong -> {
                    // 更新进度条
                    int progress = (int) (aLong * 100 / 100); // 计算当前进度
                    progressBar.setProgress(progress);
                })
                .take(100) // 只发射10次
                .subscribe(aLong -> {
                    // 倒计时结束
                    if (aLong >= 99){
                        progressBar.setProgress(100);
                        TestNetReultDialog dialog = TestNetReultDialog.newInstance();
                        dialog.setCancelable(false);
                        FragmentManager fragmentManager =getActivity().getSupportFragmentManager();
                        dialog.show(fragmentManager, "");
                        dismiss();
                    }
                }, throwable -> {
                    // 处理错误情况
                });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose(); // 取消订阅，避免内存泄漏
        }
    }



}

